<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>站点列表</title>
    <link rel="stylesheet" href="/css/index.css" type="text/css">
    <script src="/js/vue.js"></script>
    <script src="/js/axios.min.js"></script>
    <script src="/js/qs.min.js"></script>
    <script src="/js/index.js"></script>
</head>

<body>
<div id="app">
    <!-- 表单模态框区域 -->
    <el-button type="text" @click="dialogFormVisible = true">添加机构</el-button>
    <el-dialog title="订单" :visible.sync="dialogFormVisible" @close="closeDialog">
        <el-form :model="organization">
            <el-form-item label="名字">
                <el-input v-model="organization.name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="上级Id">
                <el-input v-model="organization.pid" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="用户Id">
                <el-input v-model="organization.userId" autocomplete="off"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="SaveOrganization">确 定</el-button>
        </div>
    </el-dialog>
    <el-table
            ref="multipleTable"
            :data="organizations"
            style="width: 100%">
        <el-table-column
                label="名字">
            <template slot-scope="scope">
                <sidpan style="margin-left: 10px">{{ scope.row.name }}</sidpan>
            </template>
        </el-table-column>
        <el-table-column
                label="上级Id">
            <template slot-scope="scope">
                <span style="margin-left: 10px">{{ scope.row.pid }}</span>
            </template>
        </el-table-column>
        <el-table-column
                label="用户Id">
            <template slot-scope="scope">
                <span style="margin-left: 10px">{{ scope.row.userId }}</span>
            </template>
        </el-table-column>
        <el-table-column label="操作">
            <template slot-scope="scope">
                <el-button
                        size="mini"
                        @click="handleEdit(scope.$index, scope.row)">编辑
                </el-button>
                <el-button
                        size="mini"
                        type="danger"
                        @click="handleDelete(scope.$index, scope.row)">删除
                </el-button>
            </template>
        </el-table-column>
    </el-table>
    <el-pagination
            background
            layout="prev, pager, next"
            :total="total"
            :page-size="pageSize"
            :current-page="current"
            @current-change="loadOrganizations">
    </el-pagination>
</div>

</body>
<script>
    new Vue({
        el: "#app",
        data:{
            organizations:[],
            organization:{id:0},
            total:0,
            current:0,
            dialogFormVisible:false,

        },
        methods:{
            loadOrganizations(current,size){
                axios.get("/organizations/"+current+"/"+size)
                    .then(res=>{
                        this.organizations = res.data.records;
                        this.total = res.data.total;
                        this.current = res.data.current;
                    })
            },
            SaveOrganization(){
                let organization = JSON.parse(JSON.stringify(this.organization));
                if (organization.id!==0){
                    axios.put("/organizations",organization)
                        .then(res=>{
                            if (res.data==="ok"){
                                this.dialogFormVisible=false;
                                this.$message("修改机构成功")
                                this.loadOrganizations(1,5);
                            }
                        })

                }else {
                    axios.post("/organizations",organization)
                        .then(res=>{
                            if (res.data==="ok"){
                                this.dialogFormVisible=false;
                                this.$message("添加机构成功")
                                this.loadOrganizations(1,5);
                            }
                        })
                }

            },
            closeDialog(){
                this.organization={id:0}
            },
            handleEdit(index,row){
                this.organization= JSON.parse(JSON.stringify(row));
                this.dialogFormVisible=true;
            },
            handleDelete(index,row){
                axios.delete("/organizations/"+row.id)
                    .then(res=>{
                        if (res.data==="ok"){
                            this.$message("删除机构成功")
                            this.loadOrganizations(1,5);
                        }
                    })
            }


        },
        mounted(){
            this.loadOrganizations(1,5);
        },



    })
</script>
</html>